package dai.ui.single

import android.view.View
import dai.ui.UITween


open class MoveTo(val duration: Float, var dstX: Float, var dstY: Float) : UITween() {

    var firstExecute = false

    var px = 0f
    var py = 0f
    var cx = 0f
    var cy = 0f
    var elapsedTime = 0f

    override fun initialize(node: View) {
        cx = node.x
        cy = node.y
        px = (dstX - node.x) / duration
        py = (dstY - node.y) / duration
        elapsedTime = 0f
    }

    override fun execute(dt: Float) {
        val node = target!!
        if (!firstExecute) {
            initialize(node)
            firstExecute = true
        }

        elapsedTime += dt
        if (elapsedTime >= duration) {
            node.x = dstX
            node.y = dstY
            end = true
        } else {
            cx += px * dt
            cy += py * dt
            node.x = cx
            node.y = cy
        }
    }

    override fun clone(): UITween{
        return MoveTo(duration,dstX,dstY)
    }
}

class MoveBy(duration: Float, private val dx: Float, private val dy: Float) : MoveTo(duration, 0f, 0f) {
    override fun initialize(node: View) {
        cx = node.x
        cy = node.y
        dstX = node.x + dx
        dstY = node.y + dy
        px = dx / duration
        py = dy / duration
        elapsedTime = 0f
    }

    override fun clone(): UITween {
        return MoveBy(duration,dx,dy)
    }
}